package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.repository.StrategyCatalogRepository;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.Catalogs;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;
    
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;


    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){
        // page
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page",page);

        return "strategy/list";
    }

    @RequestMapping("/input")
    public String input(Model model,String id){

        // 如果有id传过来 说明是更新操作
        if (StringUtils.hasLength(id)) {
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy",strategy);
        }

        // 攻略分类 catalogs
        List<Catalogs> cateLogs = strategyCatalogService.getStrategyCateLogs();
        model.addAttribute("catalogs",cateLogs);

        // 主题分类 themes
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes",themes);

        return "strategy/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){

//        if (StringUtils.hasLength(strategy.getId())) {
//            strategyService.update(strategy);
//        }else{
//            strategyService.save(strategy);
//        }

        strategyService.saveOrUpdate(strategy);

        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }



    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        strategyService.changeStateValue(id,state);
        return JsonResult.success();
    }



    

   

}
